package com.charm.nukaties

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.view.ViewPager
import com.charm.nukaties.myapplication.R
import kotlinx.android.synthetic.main.activity_main.*
import com.charm.nukaties.adapter.vp.ViewPagerAdapter
import com.ashokvarma.bottomnavigation.BottomNavigationBar
import com.ashokvarma.bottomnavigation.BottomNavigationItem
import com.billy.cc.core.component.CC
import com.billy.cc.core.component.CCResult


class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setViewPager()
        initFragment()
        initBottomView()
        initListener()
    }

    /**
     *
     */
    private fun initBottomView() {
        bottom_view!!
                .setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC)
        bottom_view!!
                .setMode(BottomNavigationBar.MODE_FIXED)
        var homeBottomItem = BottomNavigationItem(R.drawable.ic_emtpy_light, R.string.main_home).setActiveColorResource(R.color.bluea1)
        var loreBottomItem = BottomNavigationItem(R.drawable.ic_emtpy_light, R.string.main_market).setActiveColorResource(R.color.bluea1)
        var circleBottomItem = BottomNavigationItem(R.drawable.ic_emtpy_light, R.string.main_lore).setActiveColorResource(R.color.bluea1)
        var workBenchBottomItem = BottomNavigationItem(R.drawable.ic_emtpy_light, R.string.main_bench).setActiveColorResource(R.color.bluea1)
        bottom_view!!.inActiveColor = R.color.grey750
        bottom_view!!.activeColor = R.color.bluea1
        bottom_view!!
                .addItem(homeBottomItem)
                .addItem(loreBottomItem)
                .addItem(circleBottomItem)
                .addItem(workBenchBottomItem)
                .setFirstSelectedPosition(0)
                .initialise()
    }

    /**
     * 对ViewPager的一些设置
     */
    private fun setViewPager() {
        //禁止ViewPager滑动
        viewPager.setOnTouchListener { _, _ -> true }
    }

    /**
     *
     */
    private fun initListener() {
        viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {

            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageSelected(position: Int) {
            }

        })

        bottom_view.setTabSelectedListener(object : BottomNavigationBar.OnTabSelectedListener {
            override fun onTabReselected(position: Int) {

            }

            override fun onTabUnselected(position: Int) {
            }

            override fun onTabSelected(position: Int) {
                viewPager.setCurrentItem(position, false)
            }

        })
    }

    /**
     * 初始化Fragment
     */
    private fun initFragment() {
        var fragments = ArrayList<Fragment>()
        fragments.add(getTransactionFragment())
        fragments.add(Fragment())
        fragments.add(getUserInfoFragment())
        fragments.add(Fragment())
        val adapter = ViewPagerAdapter(fragments, supportFragmentManager)
        viewPager.adapter = adapter
    }

    /**
     *
     */
    private fun getUserInfoFragment(): Fragment {
        var frag = Fragment()
        var result = CC.obtainBuilder("userInfo")
                .setActionName("getUserInfoFragment")
                .build()
                .call()
        if (result!!.isSuccess) {
            val fragment = result.getDataItem<Fragment>("fragment")
            if (fragment != null) {
                frag = fragment
            }
        }
        return frag
    }

    /**
     *
     */
    private fun getTransactionFragment(): Fragment {
        var frag = Fragment()
        var result = CC.obtainBuilder("transaction")
                .setActionName("getTransactionFragment")
                .build()
                .call()
        if (result!!.isSuccess) {
            val fragment = result.getDataItem<Fragment>("fragment")
            if (fragment != null) {
                frag = fragment
            }
        }
        return frag
    }
}
